package cn.silver.framework.web.enums;

import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.silver.framework.core.model.DictModel;
import cn.silver.framework.core.utils.StreamUtils;
import cn.silver.framework.web.utils.UnsignedMathGenerator;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 验证码类型
 *
 * @author Silver Zhou
 */
@Getter
@AllArgsConstructor
public enum CaptchaTypeEnums {

    /**
     * 数字
     */
    MATH("math", "数学表达式", 1, UnsignedMathGenerator.class),

    /**
     * 字符
     */
    CHAR("char", "字符", 4, RandomGenerator.class),
    /**
     * 不使用验证码
     */
    NONE("none", "不使用验证码", 0, RandomGenerator.class);


    private final String code;
    private final String name;
    private final Integer length;
    private final Class<? extends CodeGenerator> clazz;

    public static CaptchaTypeEnums getByCode(String code) {
        return Arrays.stream(values()).filter(item -> code.equalsIgnoreCase(item.code)).findFirst().orElse(CaptchaTypeEnums.NONE);
    }

    public static List<DictModel> getModels() {
        return StreamUtils.toList(Arrays.asList(values()), item -> new DictModel(item.name, item.code));
    }
}

